package com.hys.authserver.auth;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;

/**
 * IDE: IntelliJ IDEA
 * 项目: auth-server
 * 日期: 2024/9/19 0:03:49
 * 作者: qscy
 * 功能:
 **/
@Setter
@Getter
@Builder
@Slf4j
public class AuthUserDetails implements UserDetails {

    private String uid;
    private String username;
    private String password;
    private String phone;
    private String code;

    //需要以ROLE_ 开头,并以逗号分割
    private String authorities;

    @Override
    public List<GrantedAuthority> getAuthorities() {
        return AuthorityUtils.createAuthorityList(StringUtils.split(authorities,","));
    }

    public String getAuthoritiesStr() {
        return authorities;
    }

    @Override
    public String getPassword() {
        return this.password;
    }

    @Override
    public String getUsername() {
        return this.username;
    }


    public static SmsAuthenticationToken toToken(AuthUserDetails user) {
        SmsAuthenticationToken token = new SmsAuthenticationToken(user.getPhone(), user.getCode(), user.getAuthorities());
        token.setUid(user.getUid());
        user.setCode(user.getCode());
        token.setCode(user.getUsername());
        token.setUsername(user.getUsername());
        token.setPassword(user.getPassword());
        token.setDetails(user);
        log.info("验证成功");
        return token;
    }
}
